/*
 * Autori: Jan Seda xsedaj00
 *         Ivo Stepanek xstepa39
 *
 * Datum: 8.5.2011
 * Popis: Zdrojovy soubor k projektu pro predmet ICP. Implementace reprezentace jednoho policka hraci desky.
 */

#include "GameSquare.h"

GameSquare::GameSquare(int _square_size, QColor _brush, QString _icon_name)
{
  circle_r = sqrt(_square_size*_square_size/2);
  square_size = _square_size;
  brush = _brush;
  icon_name = _icon_name;

  pen.setColor(Qt::black);
  pen.setWidth(NORMAL_PEN_WIDTH);
  active = false;
  setEnabled(false);
}

GameSquare::GameSquare(int _square_size, QColor _brush)
{
  circle_r = sqrt(_square_size*_square_size/2);
  square_size = _square_size;
  brush = _brush;
  icon_name = "";

  pen.setColor(Qt::black);
  pen.setWidth(NORMAL_PEN_WIDTH);
  active = false;
  setEnabled(false);
}

GameSquare::GameSquare(int _square_size)
{
  circle_r = sqrt(_square_size*_square_size/2);
  square_size = _square_size;
  brush = Qt::white;
  icon_name = "";

  pen.setColor(Qt::black);
  pen.setWidth(NORMAL_PEN_WIDTH);
  active = false;
  setEnabled(false);
}

QRectF GameSquare::boundingRect() const
{
    return QRectF(0, 0, 2*circle_r, 2*circle_r);
}

void GameSquare::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    painter->setBrush(brush);
    painter->setPen(pen);
    int d = 2 * circle_r;
    painter->drawEllipse(0, 0, d, d);
    QPixmap pix;
    if (!icon_name.isEmpty())
        pix.load(icon_name);
    int dif = circle_r - square_size/2;
    painter->drawPixmap(dif, dif,pix);
}

void GameSquare::setActive(int player)
{
    pen.setColor(color_dark[player]);
    active = true;
    this->setEnabled(true);
    pen.setWidth(BOLD_PEN_WIDTH);
    update();
}


void GameSquare::setUnactive(int player)
{
   if (active)
   {
      if (type == SQUARE_END_HOME)
      {
         if (player >=0) // policko obsazeno hracem
            brush = color_light[player];
         else
            brush = Qt::white; // policko je neobsazeno
      }
      else
      {
         if (player_occup >=0) // policko obsazeno hracem
            brush = color[player_occup];
         else
            brush = Qt::white; // policko je neobsazenoak;
      }
   }
   pen.setColor(Qt::black);
   pen.setWidth(NORMAL_PEN_WIDTH);
   active = false;
   this->setEnabled(false);
   update();
}


void GameSquare::setOccup(int player)
{
   player_occup = player;
   brush = color[player_occup];
   active = false; // je obsazeno, nikolik zvyrazneno
   pen.setColor(Qt::black);
   pen.setWidth(NORMAL_PEN_WIDTH);
   update();
}

void GameSquare::setFree()
{
   if (type == SQUARE_START_HOME)
      brush = color_light[player_occup];
   else
   {
      brush = Qt::white;
      pen.setColor(Qt::black);
   }
   player_occup = PLAYER_NOBODY;
   pen.setWidth(NORMAL_PEN_WIDTH);
   update();
}

void GameSquare::updateSquare(QString path, int home_number)
{
   SetIconName(path + QString("%1").arg(icon_number) + ".png");
   if (player_occup != PLAYER_NOBODY) // policko je obsazeno -> vybarvi se prislusnou barvou
      brush = color[player_occup];
   else
   { // policko neobsazeno -> domovska policka se vyplni prislusnou svetlou barvou
      if (type != SQUARE_SHARED)
         brush = color_light[home_number];
   }
   if (active)
   update();
}

void GameSquare::mousePressEvent(QGraphicsSceneMouseEvent * event)
{

    if (event->button() != Qt::LeftButton)
    {
      QGraphicsItem::mousePressEvent(event);
      return;
    }
    emit(squareClick(index, type, player_occup));
}
